﻿/*
 * 声明三个二维数组，实现两个矩阵相加
 */

static void MatrixAdd(int[,] arrA, int[,] arrB, int[,] arrC, int dimX, int dimY)
{
    int row, col;
    if (dimX <= 0 || dimY <= 0)
    {
        Console.WriteLine("矩阵维数必须大于0");
        return;
    }

    for (row = 1; row <= dimX; row++)
    {
        for (col = 1; col <= dimY; col++)
        {
            arrC[(row - 1), (col - 1)] = arrA[(row - 1), (col - 1)] + arrB[(row - 1), (col - 1)];
        }
    }
}

static void Main(string[] args)
{
    int i;
    int j;
    const int ROWS = 3;
    const int COLS = 3;
    int[,] A = { { 1, 3, 5 }, { 7, 9, 11 }, { 13, 15, 17 } };
    int[,] B = { { 9, 8, 7 }, { 6, 5, 4 }, { 3, 2, 1 } };
    int[,] C = new int [ROWS, COLS];
    Console.WriteLine("[矩阵A的各个元素]");
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            Console.Write(A[i, j] + "\t");
        }

        Console.WriteLine();
    }

    Console.WriteLine("[矩阵B的各个元素]");
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            Console.Write(B[i, j] + "\t");
        }

        Console.WriteLine();
    }

    MatrixAdd(A, B, C, 3, 3);
    Console.WriteLine("[显示矩阵A和矩阵B相加的结果]");
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            Console.Write(C[i, j] + "\t");
        }

        Console.WriteLine();
    }

    Console.ReadKey();
}